//
//  MLWWebView.m
//  KITCHEN_STORIES
//
//  Created by 孟令通 on 16/6/8.
//  Copyright © 2016年 MLW. All rights reserved.
//

#import "MLWWebView.h"

#import <WebKit/WebKit.h>

@interface MLWWebView ()<WKUIDelegate, WKNavigationDelegate>


@end

@implementation MLWWebView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSString *js = @"";
    
    WKUserScript *userScript = [[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    
    config.preferences.minimumFontSize = 14;
    
    config.preferences.javaScriptEnabled = YES;
    
    config.userContentController = [[WKUserContentController alloc] init];
    
    [config.userContentController addUserScript:userScript];
    
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 49) configuration:config];
    
    webView.UIDelegate = self;
    webView.navigationDelegate = self;
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    
    [self.view addSubview:webView];
    [webView release];
    
}

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    
    // 判断为网上厨房的链接
    if ([[[self.url componentsSeparatedByString:@"?"] objectAtIndex:0] isEqualToString:[[DETAIL_URL(@"a") componentsSeparatedByString:@"?"] objectAtIndex:0]] ) {
        
        [webView evaluateJavaScript:@"document.getElementsByTagName('div')[0].style.display = 'none'" completionHandler:nil];
        
        [webView evaluateJavaScript:@"document.getElementById('footer').style.display = 'none'" completionHandler:nil];
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
